<?php
/**
 * 商品中心逻辑处理模块
 * User:LLW
 * Date:2020-03-26
 */

namespace app\api\model;

use think\Db;
use think\Model;

class GoodsModel extends Model
{
    protected $table = 'yc_goods';

    /**
     * 商品列表
     * @param $caid 分类id
     * @return object
     */
    public function getList()
    {
        $caid = input('post.caid',null);
        $list = $this->where(['go_sale'=>1,'go_caid'=>$caid])
            ->field('go_id,go_name,go_price,go_score,go_recom,go_new,go_hot,go_thumb')
            ->order('go_stime desc')->select();
        AjaxReturn(1,'ok',$list);
    }

    /**
     * 商品分类
     * @param $type 1:特价超市 2：抢先兑
     * @return object
     */
    public function getClass()
    {
        $type = input('get.type',1);
        //商品分类
        $list = Db::name('category')->where(['ca_parent'=>$type,'ca_show'=>1])->order('ca_sort asc')->select();
        $i = 0;
        foreach ($list as $key=>$val){
            $i += 1;
            $list[$key]['id'] = $i;
        }
        AjaxReturn(1,'ok',$list);
    }

    /**
     * 商品详情
     * @param $id 商品id
     * @return object
     */
    public function getFind()
    {
        $id = input('post.id',null);
        $find = $this->where('go_id',$id)->find();
        if(empty($find)){
            AjaxReturn(-1,'商品信息错误');
        }
        $find['go_imgs'] = array_filter(explode(',',$find['go_imgs']));
        $find['evaluation'] = Db::name('order_evaluation')->where(['oc_gid'=>$id,'oc_state'=>1])->count();
        $find['go_body'] = htmlspecialchars_decode($find['go_body']);
        AjaxReturn(1,'ok',$find);
    }

    /**
     * 商品评价
     * @param $id 商品id
     * @param $img 查看有图评价
     * @param $agree 查看4星以上评价
     * @param $time 查看最新评价
     * @return object
     */
    public function getComments()
    {
        $id = input('post.id',0);
        $type = input('post.type',1);
        $where = [];
        $where['oc_state'] = 1;
        $where['oc_gid'] = $id;
        if($type == 1){
             $where = [];
        }elseif ($type == 3){
            $where['oc_agree'] = array('GT',3);
        }
        $list = Db::name('order_evaluation')->alias('e')
            ->join('yc_user u','e.oc_uid=u.us_uid')
            ->where($where)->field('e.*,u.us_nickname,u.us_head');
        if($type == 4){
            $list = $list->order('oc_time desc');
        }
        $lists = $list->select();
        foreach ($lists as $key=>$val) {
            $lists[$key]['oc_time'] =  date('Y-m-d',$val['oc_time']);
            $lists[$key]['oc_capimg'] = array_filter(explode(',',$val['oc_capimg']));
        }
        AjaxReturn(1,'ok',$lists);
    }
}